package io.renren.common.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;
import io.renren.common.utils.Result;
import io.renren.modules.sp.dto.SpTeacherCerImport;
import io.renren.modules.sp.service.SpTeacherCerImportService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.List;

/**
 * 指导老师荣誉证书导入
 */
@Slf4j
public class SpTeacherCerImportListener extends AnalysisEventListener<SpTeacherCerImport> {

    private Result result;

    private SpTeacherCerImportService spCerTeacherService;
    /**
     * 每隔5条存储数据库，实际使用中可以3000条，然后清理list ，方便内存回收
     */
    private static final int BATCH_COUNT = 3000;
    private static int PROCESS, TOTAL = 0;
    List<SpTeacherCerImport> list;

    public SpTeacherCerImportListener(SpTeacherCerImportService userService, Result result, List<SpTeacherCerImport> list) {
        this.spCerTeacherService = userService;
        this.result = result;
        this.list = list;
    }

    @Override
    public void invoke(SpTeacherCerImport data, AnalysisContext context) {
        log.info("解析到一条数据:{}", JSON.toJSONString(data));
        PROCESS++;
        list.add(data);
        // 达到BATCH_COUNT了，需要去存储一次数据库，防止数据几万条数据在内存，容易OOM
        if (list.size() >= BATCH_COUNT) {
            saveData();
            list.clear();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        saveData();
        TOTAL = 0;
        PROCESS = 0;
        log.info("所有数据解析完成！");
    }

    /**
     * 加上存储数据库
     */
    private void saveData() {
        log.info("{}条数据，开始存储数据库！", list.size());
        if (!CollectionUtils.isEmpty(list)) {
            List<SpTeacherCerImport> saveList = new ArrayList();
            // 校验list中的Certificate字段是否重复
            List<String> certificateList = new ArrayList<>();
            for (SpTeacherCerImport spCertificateImport : list) {
                if (certificateList.contains(spCertificateImport.getCertificate())) {
                    result.setCode(0);
                    result.setData("error");
                    result.setMsg(spCertificateImport.getCertificate() + "证书编号重复");
                    return;
                }
                certificateList.add(spCertificateImport.getCertificate());
            }

            for (SpTeacherCerImport spCerTeacherImport : list) {
                spCerTeacherImport = spCerTeacherService.checkCertificate(spCerTeacherImport);
                if (StringUtils.isNotEmpty(spCerTeacherImport.getCheckMsg())) {
                    result.setCode(0);
                    result.setData("error");
                    result.setMsg(spCerTeacherImport.getCheckMsg());
                }
                saveList.add(spCerTeacherImport);
            }
            for (SpTeacherCerImport spCertificateImport : list) {
                if (StringUtils.isNotEmpty(spCertificateImport.getCheckMsg())) {
                    return;
                }
            }
            try {
                spCerTeacherService.saveCertificateImport(saveList);
                TOTAL += saveList.size();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        log.info("存储数据库成功！");
    }
}
